package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {

    /*@RabbitListener (queues = "simple.queue")
    public void listenerSimpleQueueMessage(String msg) throws InterruptedException{
        System.out.println("接收消息："+msg);

    }*/
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener (queues = "fanout.queue1")
    public void fanoutQueue1Message(String msg) {
        System.out.println("fanout.queue1接收消息："+msg);

    }
    @RabbitListener (queues = "fanout.queue2")
    public void fanoutQueue2Message(String msg) {
        System.out.println("fanout.queue2接收消息："+msg);

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue (name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"blue","red"}
    ))
    public void directQueue1Message(String msg) {
        System.out.println("direct.queue1接收消息："+msg);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue (name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"yellow","red"}
    ))
    public void directQueue2Message(String msg) {
        System.out.println("direct.queue2接收消息："+msg);

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void topicQueue1Message(String msg) {
        System.out.println("topic.queue1接收消息："+msg);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void topicQueue2Message(String msg) {
        System.out.println("topic.queue2接收消息："+msg);

    }

    @RabbitListener(queues = "object.queue")
    public void objectQueueMessage(Map<String,Object> msg) {
        System.out.println("object.queue接收消息："+msg);

    }
}
